package com.cyz.springdemo.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
 * @program: springdemo
 * @description: 测试过滤器
 * @注意：配置过滤器有两种方法：
 * 一、将过滤器HelloFilter注册成一个Bean,具体操作看/config/FilterConfig.java
 * 二、使用Servlet 3.0规范的@WebFilter直接将HelloFilter配置成；过滤器，操作如下:
 *      urlPatterns  ------>过滤的url
 *      filterName   ------>过滤器的名字
 *      出来@WebFilter这个注解以外，还要在启动类SpringdemoApplication中添加注解@ServletComponetScan,指定扫描的包"com.cyz.springdemo.filter"
 * @author: wangjie
 * @create: 2019-07-25 16:56
 **/
@WebFilter(urlPatterns = "/hello/index",filterName = "helloFilter")
public class HelloFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        Map<String,String[]> map = request.getParameterMap();
        map.forEach((k,v) ->{
            System.out.println(k + "=" + Arrays.toString(v));
        });
    }

    @Override
    public void destroy() {

    }
}
